package com.sc.nft.dao;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.DaoSchoolArticleChild;
import com.sc.nft.entity.dto.DaoSchoolArticleChildPageAdminDTO;
import com.sc.nft.entity.dto.DaoSchoolArticleChildSaveAdminDTO;
import com.sc.nft.entity.dto.DaoSchoolArticleChildUpdateSortAdminDTO;
import com.sc.nft.entity.dto.DaoSchoolHomeArticleListDTO;
import com.sc.nft.entity.vo.DaoSchoolArticleChildPageAdminVO;
import com.sc.nft.entity.vo.DaoSchoolTopHomeArticlePageVO;
import com.sc.nft.mapper.DaoSchoolArticleChildMapper;
import com.sc.nft.pool.SysMemberPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章表与子分类关联表(DaoSchoolArticleChild)表Dao层
 *
 * @author makejava
 * @since 2023-03-13 16:21:08
 */
@Slf4j
@Repository
public class DaoSchoolArticleChildDao extends ServiceImpl<DaoSchoolArticleChildMapper, DaoSchoolArticleChild> {

    /**
     * 根据文章id获取所有数据
     *
     * @param id
     * @return
     */
    public List<DaoSchoolArticleChild> selectByArticleId(Long id) {
        QueryWrapper<DaoSchoolArticleChild> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", id);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 获取关联的所有文章
     *
     * @param daoSchoolArticleChildPageAdminDTO
     * @return
     */
    public Page<DaoSchoolArticleChildPageAdminVO> pageAdmin(DaoSchoolArticleChildPageAdminDTO daoSchoolArticleChildPageAdminDTO) {

        return baseMapper.pageAdmin(daoSchoolArticleChildPageAdminDTO.toMp(), daoSchoolArticleChildPageAdminDTO);
    }

    /**
     * 删除关联
     *
     * @param id
     * @return
     */
    public Boolean deleteById(Long id) {
        return SqlHelper.retBool(baseMapper.deleteById(id));
    }

    /**
     * 文章关联新增
     *
     * @param info
     * @return
     */
    public Boolean saveAdmin(List<DaoSchoolArticleChildSaveAdminDTO> info) {
        List<DaoSchoolArticleChild> list = new ArrayList<>();
        info.forEach(daoSchoolArticleChildSaveAdminDTO -> {
            DaoSchoolArticleChild daoSchoolArticleChild = new DaoSchoolArticleChild();
            BeanUtil.copyProperties(daoSchoolArticleChildSaveAdminDTO, daoSchoolArticleChild);
            daoSchoolArticleChild.setCreateId(SysMemberPool.getMember().getId());
            daoSchoolArticleChild.setModifyId(SysMemberPool.getMember().getId());
            list.add(daoSchoolArticleChild);
        });
        return SqlHelper.retBool(baseMapper.saveAdmin(list));
    }

    /**
     * 获取置顶的文章
     *
     * @param dto
     * @return
     */
    public Page<DaoSchoolTopHomeArticlePageVO> getHomeArticleList(DaoSchoolHomeArticleListDTO dto) {
        return baseMapper.getHomeArticleList(dto.toMp(), dto,6L);
    }


    public Boolean updateSortById(DaoSchoolArticleChildUpdateSortAdminDTO dto) {
        return baseMapper.updateSortById(dto);
    }
}
